#include "course.h"

using namespace std;

int Course::CourseCnt = 0;

// Implementation of the constructor
Course::Course(string name, string nature, string disc, string time, int lab, float cred, int sem)
    : courseName(name), courseNature(nature), sort(disc), classTime(time),
      labHours(lab), credit(cred), semester(sem) {
    this->courseId = to_string(++Course::CourseCnt);
    this->totalHours = calculateTotalHours(classTime);
    this->teachingHours = totalHours - labHours;
}

// Copy constructor
Course::Course(const Course& other)
    : courseId(other.courseId), courseName(other.courseName), courseNature(other.courseNature),
      sort(other.sort), classTime(other.classTime), totalHours(other.totalHours),
      teachingHours(other.teachingHours), labHours(other.labHours), credit(other.credit),
      semester(other.semester) {
}

// Assignment operator overloading
Course& Course::operator=(const Course& other) {
    if (this != &other) {
        courseId = other.courseId;
        courseName = other.courseName;
        courseNature = other.courseNature;
        sort = other.sort;
        classTime = other.classTime;
        totalHours = other.totalHours;
        teachingHours = other.teachingHours;
        labHours = other.labHours;
        credit = other.credit;
        semester = other.semester;
    }
    return *this;  // Return a reference to the current object to support chained assignments
}

// Calculate total hours based on classTime
int Course::calculateTotalHours(const string& time) const {
    stringstream ss(time);
    string session;
    int weeklyHour = 0;
    int totalHours = 0;

    while (ss >> session) {
        int start = session[1] - '0';  // '1' -> 1
        int end = session[2] - '0';    // '3' -> 3
        if (end == 0) {
            end = 10;
        }
        
        // Calculate hours for each session
        weeklyHour += end - start + 1;
    }
    totalHours = weeklyHour * 16;  // Assuming a 16-week semester

    return totalHours;
}

// Get course information
string Course::toString() const {
    stringstream ss;
    ss << "ID: " << courseId << ", Name: " << courseName
       << ", Nature: " << courseNature << ", Discipline: " << sort
       << ", Time: " << classTime << ", Total Hours: " << totalHours
       << ", Teaching Hours: " << teachingHours << ", Lab Hours: " << labHours
       << ", Credit: " << credit << ", Semester: " << semester;
    return ss.str();
}

